package com.sky.wsp.sql.scanner.patterns;

import com.sky.wsp.sql.scanner.patterns.func.Mysql2DmFuncPattern;
import com.sky.wsp.sql.scanner.patterns.func.Mysql2KingbaseFuncPattern;
import com.sky.wsp.sql.scanner.patterns.sql.AnnotationPattern;
import com.sky.wsp.sql.scanner.patterns.sql.MapperPattern;
import com.sky.wsp.sql.scanner.patterns.sql.PhpPattern;
import com.sky.wsp.sql.scanner.patterns.sql.PythonPattern;
import com.sky.wsp.sql.scanner.vo.MatchResult;

import java.util.Collections;
import java.util.List;

/**
 * @author wangshaopeng@talkweb.com.cn
 * @desccription 在此输入描述
 * @date 2024/11/13
 */
public class PatternManager {
    private static AnnotationPattern annotationPattern = new AnnotationPattern();
    private static MapperPattern mapperPattern = new MapperPattern();
    private static PhpPattern phpPattern = new PhpPattern();
    private static PythonPattern pythonPattern = new PythonPattern();

    public static List<MatchResult> matchSQL(String fileName, String content) {
        int dot = fileName.lastIndexOf(".");
        if (dot < 0) {
            return Collections.emptyList();
        }
        String ext = fileName.substring(dot + 1);
        List<MatchResult> matchResults;
        switch (ext) {
            case "java":
                matchResults = annotationPattern.match(content);
                break;
            case "xml":
                matchResults = mapperPattern.match(content);
                break;
            case "php":
                matchResults = phpPattern.match(content);
                break;
            case "python":
                matchResults = pythonPattern.match(content);
                break;
            case "class":
            case "jar":
            case "yml":
            case "properties":
                matchResults = Collections.emptyList();
                // 这几种类型不做任何处理，也不打日志
                break;
            default:
//                System.err.println("PatternManager.matchSQL 暂不支持的文件类型：" + ext);
                matchResults = Collections.emptyList();
        }
        return matchResults;
    }


    private static Mysql2DmFuncPattern mysql2DmFuncPattern = new Mysql2DmFuncPattern();

    public static List<MatchResult> matchMysql2DmFunc(String filename, String content) {
        List<MatchResult> matchResults = mysql2DmFuncPattern.match(content);
        return matchResults;
    }

    private static Mysql2KingbaseFuncPattern mysql2KingbaseFuncPattern = new Mysql2KingbaseFuncPattern();

    public static List<MatchResult> matchMysql2KingbaseFunc(String filename, String content) {
        List<MatchResult> matchResults = mysql2KingbaseFuncPattern.match(content);
        return matchResults;
    }
}
